﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Windows.Forms;
using AuctionSystem;
using AuctionSystem.AuctionService;

namespace AuctionSystem
{
    public partial class CreateAuction : Form
    {
        private AuctionServiceClient _client;

        public CreateAuction()
        {
            InitializeComponent();
            MyCallBack cb = new MyCallBack();
            InstanceContext context = new InstanceContext(cb);

            try
            {
                _client = new AuctionServiceClient(context, "TcpBinding");
                if (_client.State != CommunicationState.Faulted) { _client.Open(); }
                if (_client.State == CommunicationState.Opened)
                {
                    
                }
            }
            catch (Exception)
            {
                MessageBox.Show("Service is not running. Try again!");
                Environment.Exit(0);
            }

            txtDatePicker.MinDate = DateTime.Today;
        }


        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            var list = new List<Item>();
            var auction = new Auction();
            auction.ID = 0;
            auction.Title = txtTitle.Text;
            auction.Description = txtDescription.Text;
            DateTime date = txtDatePicker.Value.Date;
            String.Format("{0:yyyy-MM-dd}", date);
            auction.Date = date;

            foreach (var item in itemsList.Items)
            {
                list.Add((Item)item);
            }

            _client.CreateAuction(auction, list);
        }

        private void btnAddItem_Click(object sender, EventArgs e)
        {
            ItemsList itemsList = new ItemsList(_client);
            itemsList.Show();

        }

        private void btnDeleteItem_Click(object sender, EventArgs e)
        {

        }

    }
}
